/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmAdministrator.java
 *
 * Created on 01-03-2011, 22:02:04
 */
package GUI;

import interact.ActionAdministrator;
import interact.DataInteraction;
import interact.LoginInteraction;
import interact.ValidateData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 *
 * @author UnderGround
 */
public class frmAdministrator extends javax.swing.JPanel {

    java.util.Date d = null;
    java.sql.Date sqlD = null;

    /** Creates new form frmAdministrator */
    public frmAdministrator() {
        initComponents();
        loadData();



        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(frmAdministrator.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(frmAdministrator.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(frmAdministrator.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(frmAdministrator.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void loadData() {
        try {
            ResultSet rs = DataInteraction.setAdminInfo();
            while (rs.next()) {
                this.id = rs.getString("UserID");
                txtUsername.setText(rs.getString("UserName"));
                txtFullname.setText(rs.getString("FullName"));
                txtPhone.setText(rs.getString("Phone"));
                txtEmail.setText(rs.getString("Email"));
                gender = rs.getInt("Gender");
                System.out.println("check"+rs.getString("Email"));
                if (gender == 1) {
                    rbMale.setSelected(true);
                }
                if (gender == 0) {
                    rbFemale.setSelected(true);
                }
                 DateChooser.setDate(ValidateData.convertStringToDate(rs.getString("BirthDay")));
            }

        } catch (SQLException ex) {
            Logger.getLogger(frmAdministrator.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup = new javax.swing.ButtonGroup();
        jDesktopPane1 = new javax.swing.JDesktopPane();
        jPanel1 = new javax.swing.JPanel();
        txtEmail = new javax.swing.JTextField();
        DateChooser = new com.toedter.calendar.JDateChooser(sqlD, "dd-MM-yyyy");
        rbFemale = new javax.swing.JRadioButton();
        rbMale = new javax.swing.JRadioButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        btnUpdate = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        txtPhone = new javax.swing.JTextField();
        txtFullname = new javax.swing.JTextField();
        txtUsername = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        lblFullname = new javax.swing.JLabel();
        lblUser = new javax.swing.JLabel();

        setBackground(new java.awt.Color(204, 255, 255));

        jDesktopPane1.setBackground(new java.awt.Color(153, 153, 153));

        jPanel1.setBackground(new java.awt.Color(255, 255, 204));
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("My account Infomation"));

        buttonGroup.add(rbFemale);
        rbFemale.setText("Female");

        buttonGroup.add(rbMale);
        rbMale.setText("Male");

        jLabel2.setText("Email");

        jLabel4.setText("Birth date");

        btnUpdate.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/up.png"))); // NOI18N
        btnUpdate.setText("Update");
        btnUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUpdateActionPerformed(evt);
            }
        });

        jLabel5.setText("Gender");

        jLabel3.setText("Phone");

        lblFullname.setText("Fullname");

        lblUser.setText("User name");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(18, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(lblUser, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 60, Short.MAX_VALUE)
                    .addComponent(lblFullname, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtPhone, javax.swing.GroupLayout.DEFAULT_SIZE, 140, Short.MAX_VALUE)
                    .addComponent(txtUsername, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 140, Short.MAX_VALUE)
                    .addComponent(txtFullname, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 140, Short.MAX_VALUE))
                .addGap(64, 64, 64)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(14, 14, 14))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(18, 18, 18)))
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(DateChooser, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(txtEmail, javax.swing.GroupLayout.DEFAULT_SIZE, 170, Short.MAX_VALUE))
                        .addGap(60, 60, 60))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 100, Short.MAX_VALUE)
                        .addComponent(rbMale, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(rbFemale, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(66, 66, 66))))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(241, 241, 241)
                .addComponent(btnUpdate, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(259, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(92, 92, 92)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(lblUser))
                .addGap(39, 39, 39)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtFullname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4)
                            .addComponent(lblFullname))
                        .addGap(30, 30, 30)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(rbFemale)
                            .addComponent(rbMale)
                            .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtPhone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(DateChooser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 63, Short.MAX_VALUE)
                .addComponent(btnUpdate)
                .addGap(61, 61, 61))
        );

        jPanel1.setBounds(50, 50, 620, 400);
        jDesktopPane1.add(jPanel1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jDesktopPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 715, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jDesktopPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 490, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUpdateActionPerformed
        username = txtUsername.getText().trim();
        fullname = txtFullname.getText().trim();
        email = txtEmail.getText().trim();
        phone = txtPhone.getText().trim();

        if (rbFemale.isSelected()) {
            gender = 0;
        }
        if (rbMale.isSelected()) {
            gender = 1;
        }
        if(!ValidateData.checkEmpty(username, txtUsername)){
            ValidateData.showMessage("Please enter user name");
            return;
        }
        if(!ValidateData.checkEmpty(fullname, txtFullname)){
            ValidateData.showMessage("Please enter full name");
            return;
        }
        if(!ValidateData.checkEmpty(email, txtEmail)){
            ValidateData.showMessage("Please enter email");
            return;
        }else if(!ValidateData.isEmail(email,txtEmail)){
            ValidateData.showMessage("Email invalid. please enter again !");
            return;
        }
        try {
              birthday = ValidateData.convertDateToString(DateChooser.getDate());
        } catch (NullPointerException e) {
            ValidateData.showMessage("Birth day invalid ");
            DateChooser.requestFocus();
            DateChooser.setBackground(new java.awt.Color(250,255,168));
            return;
        }
        if(!ValidateData.checkEmpty(phone, txtPhone)){
            ValidateData.showMessage("Please enter phone");
            return;
        }else if(!ValidateData.isPhone(phone, txtPhone)){
            ValidateData.showMessage("Phone number invalid");
                return;
        }
        else{
        Vector update = new Vector();
        update.addElement(id);
        update.addElement(username);
        update.addElement(fullname);
        update.addElement(phone);
        update.addElement(email);
        update.addElement(birthday);
        update.addElement(gender);

        DataInteraction.updateData("update_Admin", update);
        ValidateData.showMessage("Update sucessful");
         }
       
}//GEN-LAST:event_btnUpdateActionPerformed

    private void fillUserInformation(String UserID) {
        if (vtUsers.size() > 0) {
            for (int i = 0; i < vtUsers.size(); i++) {
                Vector temp = (Vector) vtUsers.elementAt(i);
                if (UserID.equals(temp.elementAt(0).toString())) {
                    txtUsername.setText(temp.elementAt(1).toString());
                    txtFullname.setText(temp.elementAt(3).toString());
                    txtPhone.setText(temp.elementAt(4).toString());
                    txtEmail.setText(temp.elementAt(5).toString());
                    DateChooser.setDateFormatString(temp.elementAt(6).toString());
                    DateChooser.setMaxSelectableDate(DateChooser.getDate());
                    if (temp.elementAt(7).toString().equals("1")) {
                        rbMale.setSelected(true);
                    }
                    System.out.println(temp.elementAt(7));
                    if (temp.elementAt(7).toString().equals("0")) {
                        rbFemale.setSelected(true);
                    }

                }
            }
        }
    }
    private String username, password, fullname, phone, email, birthday, id;
    int gender;
    int rowSelected;
    Vector vtUsers = LoginInteraction.getUserInfo("select * from tblAdministrator");
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private com.toedter.calendar.JDateChooser DateChooser;
    private javax.swing.JButton btnUpdate;
    private javax.swing.ButtonGroup buttonGroup;
    private javax.swing.JDesktopPane jDesktopPane1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel lblFullname;
    private javax.swing.JLabel lblUser;
    private javax.swing.JRadioButton rbFemale;
    private javax.swing.JRadioButton rbMale;
    private javax.swing.JTextField txtEmail;
    private javax.swing.JTextField txtFullname;
    private javax.swing.JTextField txtPhone;
    private javax.swing.JTextField txtUsername;
    // End of variables declaration//GEN-END:variables
}
